#pragma once

#include "my_defs.h"
#include "my_mem.h"
#include "my_queue.h"
#include "my_spinlock.h"


typedef void (*my_cleanup_handler_pt)(void *data);

typedef struct my_cleanup_item_s my_cleanup_item_t;

struct my_cleanup_item_s {
    my_cleanup_handler_pt           handler;
    void                            *data;
    my_queue_t                      queue;
};


typedef struct {
    my_spinlock_t                   lock;
    my_queue_t                      head;
} my_cleanup_list_t;


extern my_cleanup_list_t            g_cleanup_list;


void my_cleanup_list_init(my_cleanup_list_t *list);

my_cleanup_item_t *my_cleanup_list_add(
    my_cleanup_list_t *list, my_cleanup_handler_pt handler, void *data);

void my_cleanup_list_del(my_cleanup_list_t *list, my_cleanup_item_t *item);

void my_cleanup_list_exec(my_cleanup_list_t *list);